import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
 *@author Shabda Raaj and Ashwini Kumar
 *This class represents the drawing area
 *and methods to manipulate it.
 *Here we extend JLabel and override its paintComponent().
 *This class contains a ArrayList data in which we store the 
 *CoOrdinates of points between which we need to draw lines.
 *The methods forward backward will add new coords to the list
 *PiantComponent paints lines between these points
 **/

public class DrawArea extends JLabel{
	java.util.List l=new ArrayList();//lines to draw
	Turtle t=new Turtle();
	final Color background=Color.white;
	final Color ink=Color.black;
	public DrawArea(){
		super();
		Dimension d=new Dimension(600,300);
		setPreferredSize(d);
		//l.add(new CoOrds(t.xPos,t.yPos));
		}
	public void makeBlank(){
		l.clear();
		}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(background);
		g.fillRect(0,0,getWidth(),getHeight());
		g.setColor(Color.blue);
		g.fillOval(t.xPos-2,t.yPos-2,4,4);//paint the turtle
		//now lets paint the lines
		g.setColor(ink);
		for(ListIterator i=l.listIterator();i.hasNext();){
			OrderedCoOrds o=(OrderedCoOrds)i.next();
			g.setColor(o.color);
			g.drawLine(o.first.x,o.first.y,o.sec.x,o.sec.y);
			}
		}
	public void forward(int moves){//move forward
		System.out.println("forward"+moves);
		int endx=t.xPos+(int)(moves*Math.cos(t.orien*Math.PI/180+3*Math.PI/2));
		int endy=t.yPos+(int)(moves*Math.sin(t.orien*Math.PI/180+3*Math.PI/2));
		CoOrds pos1=new CoOrds(t.xPos,t.yPos);
		CoOrds pos2=new CoOrds(endx,endy);//add new coordinates to list
		if(t.pen==0){
			OrderedCoOrds c=new OrderedCoOrds(pos1,pos2,ink);
			l.add(c);}//pen=0 pendown
		if(t.pen==1){
			OrderedCoOrds c=new OrderedCoOrds(pos1,pos2,background);
			l.add(c);
			}//pen =1 penerase
		t.xPos=endx;
		t.yPos=endy;
		repaint();
		}
	public void backward(int moves){//move backwards
		System.out.println("backward "+moves);
		int endx=t.xPos-(int)(moves*Math.cos(t.orien*Math.PI/180+3*Math.PI/2));
		int endy=t.yPos-(int)(moves*Math.sin(t.orien*Math.PI/180+3*Math.PI/2));
	    CoOrds pos1=new CoOrds(t.xPos,t.yPos);
		CoOrds pos2=new CoOrds(endx,endy);//add new coordinates to list
		if(t.pen==0){
			OrderedCoOrds c=new OrderedCoOrds(pos1,pos2,ink);
			l.add(c);}//pen=0 pendown
		if(t.pen==1){
			OrderedCoOrds c=new OrderedCoOrds(pos1,pos2,background);
			l.add(c);
			}//pen =1 penerase
		t.xPos=endx;
		t.yPos=endy;
		repaint();
		}
	public void rightTurn(int turn){
		System.out.println("right "+turn);
		t.orien+=turn;
		}
	public void leftTurn(int turn){
		System.out.println("left "+turn);
		t.orien-=turn;
		}
	public void home(){
		int endx=t.initialx;
		int endy=t.initialy;
		CoOrds pos1=new CoOrds(t.xPos,t.yPos);
		CoOrds pos2=new CoOrds(endx,endy);//add new coordinates to list
		if(t.pen==0){
			OrderedCoOrds c=new OrderedCoOrds(pos1,pos2,ink);
			l.add(c);}//pen=0 pendown
		if(t.pen==1){
			OrderedCoOrds c=new OrderedCoOrds(pos1,pos2,background);
			l.add(c);
			}//pen =1 penerase
		t.xPos=endx;
		t.yPos=endy;
		t.orien=0;
		repaint();
		//t.xPos=
		}
	public void penUp(){
		t.pen=2;
		}
	public void penDown(){
		t.pen=0;
		}
	public void penErase(){
		t.pen=1;
		}
	public static void main(String[] args){
		JFrame frame=new JFrame();
		DrawArea d=new DrawArea();
		frame.getContentPane().add(d);
		frame.pack();
		frame.show();
		}
	
	}			